package com.yupi.springbootinit.manager;

import io.github.briqt.spark4j.SparkClient;
import io.github.briqt.spark4j.constant.SparkApiVersion;
import io.github.briqt.spark4j.exception.SparkException;
import io.github.briqt.spark4j.model.SparkMessage;
import io.github.briqt.spark4j.model.SparkSyncChatResponse;
import io.github.briqt.spark4j.model.request.SparkRequest;
import io.github.briqt.spark4j.model.response.SparkTextUsage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AIManager {
    @Resource
    private SparkClient client;

    /**
     * AI 对话
     * @param message ：用户输入信息
     * @param systemContent ：预设模型信息
     *
     * @return
     */


    public String AIChat(String systemContent,String message){
        // 消息列表，可以在此列表添加历史对话记录
        List<SparkMessage> messages=new ArrayList<>();
        messages.add(SparkMessage.systemContent(systemContent));
        messages.add(SparkMessage.userContent(message));
        // 构造请求
        SparkRequest sparkRequest=SparkRequest
                .builder()
                .messages(messages)   // 消息列表
        // 模型回答的tokens的最大长度,非必传，默认为2048。
                .maxTokens(8000)
                .apiVersion(SparkApiVersion.V3_5)
                .temperature(0.5)
                .build();
        String content = null;
        try {
            // 同步调用
            SparkSyncChatResponse chatResponse = client.chatSync(sparkRequest);
            content = chatResponse.getContent();
            log.info("AI回答了：{}",content);

        } catch (SparkException e) {
            System.out.println("发生异常了：" + e.getMessage());
        }
        return content;
    }

}
